package main

import (
	amqp "github.com/rabbitmq/amqp091-go"
	"log"
)

func main() {
	// 1. 建立连接
	conn, _ := amqp.Dial("amqp://guest:guest@129.211.6.176:5672/")
	defer conn.Close()
	// 2. 创建信道
	channel, _ := conn.Channel()
	defer channel.Close()
	// 3. 声明队列
	queue, _ := channel.QueueDeclare(
		"hello",
		false,
		false,
		false,
		false,
		nil,
	)
	// 4. 获取消息
	msgs, err := channel.Consume(
		queue.Name,
		"",
		true,
		false,
		false,
		false,
		nil,
	)
	if err != nil {
		panic(err)
	}
	// 5. 启动协程处理消息
	var forever chan struct{}
	go func() {
		for msg := range msgs {
			log.Printf("Received a message: %s", msg.Body)
		}
	}()
	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
}
